﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;

namespace TaiwuZD
{
   public  class MarqueueLabel:Control
    {

        private  DispatcherTimer timer = new DispatcherTimer();

        public MarqueueLabel()
        {
            ClipToBounds = true;
            timer.Interval = new TimeSpan(0,0,0,0,500);
            //timer.Tick += Timer_Tick;
            timer.Start();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            if(Text.Width>AvalableWidth)
            {
                Offset -= 4;
                if (-Offset + AvalableWidth > Text.Width)
                    Offset = 0;

                InvalidateVisual();
            }
            
        }

        public string Content
        {
            get { return (string)GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Content.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ContentProperty =
            DependencyProperty.Register("Content", typeof(string), typeof(MarqueueLabel), new PropertyMetadata(""));


        private FormattedText Text { get; set; }


        private double AvalableWidth { get; set; }
        private double AvalableHeight { get; set; }

        private double TextWidth { get; set; }
        private double TextHeight { get; set; }

        private double Offset { get; set; }

        protected override Size MeasureOverride(Size constraint)
        {
            AvalableWidth = Math.Min(100,constraint.Width);// - Margin.Left - Margin.Right - Padding.Left - Padding.Right;
            AvalableHeight = Math.Min(100, constraint.Height);// - Margin.Top - Margin.Bottom - Padding.Top - Padding.Bottom;
            return base.MeasureOverride(constraint);
        }


        protected override void OnRender(DrawingContext drawingContext)
        {
            var text = Content;
            if (text.Length == 4)
                text = text.Insert(2, "\n");
            else if (text.Length > 4)
                text = text.Insert(3, "\n");

            Text = new FormattedText(text , System.Globalization.CultureInfo.CurrentCulture, 
                System.Windows.FlowDirection.LeftToRight, new Typeface("宋体"),FontSize,Foreground);

            TextWidth = Text.Width;
            TextHeight = Text.Height;



            //if (TextWidth > AvalableWidth && (Math.Floor(TextWidth / AvalableWidth)+ (TextWidth%AvalableWidth==0?0:1)) * TextHeight < AvalableHeight) 
            {
                Text.MaxTextWidth = AvalableWidth;
                Text.MaxTextHeight = AvalableHeight;
            }
             
            Text.TextAlignment = TextAlignment.Center;

            Text.Trimming = TextTrimming.None;

            Point pt = new Point(0, /* new Point((AvalableWidth - Text.Width) / 2 ,*/ (AvalableHeight - Text.Height) / 2);
            drawingContext.DrawText(Text, pt); 
        }
    }


    public class WrapLabel:Control
    {

        public WrapLabel()
        {
            Background = Brushes.Transparent;
        }

        public string Content
        {
            get { return (string)GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Content.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ContentProperty =
            DependencyProperty.Register("Content", typeof(string), typeof(WrapLabel), new PropertyMetadata(string.Empty));

        private FormattedText _FormattedText;
        private FormattedText FormattedText
        {
            get
            {
                if (_FormattedText == null||_FormattedText.Text != Content)
                    _FormattedText = new FormattedText(Content, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(Global.Inst.FontName), FontSize, Foreground);
                return _FormattedText;
            }
        }


        private double AvalableWidth { get; set; }
        private double AvalableHeight { get; set; }

        private double ArrangeWidth { get; set; }
        private double ArrangeHeight { get; set; }


        private double LeftPos { get { return Margin.Left + Padding.Left; } }
        private double TopPos { get { return Margin.Top + Padding.Top; } }
        private double RightPos { get { return Math.Min(ArrangeWidth,AvalableWidth) -Margin.Right - Padding.Right; } }
        private double BottomPos { get { return Math.Min(ArrangeHeight, AvalableHeight) - Margin.Bottom - Padding.Bottom; } }

        protected override Size MeasureOverride(Size constraint)
        {
            Console.WriteLine($"{Content}:MeasureOverride={constraint.Width},{constraint.Height}");
            AvalableWidth = constraint.Width;
            AvalableHeight = constraint.Height;

            //需要wrap
            if (FormattedText.Width > RightPos - LeftPos)
            {
                FormattedText.MaxTextWidth = RightPos - LeftPos;
            }


            return new Size(FormattedText?.Width??0 + Margin.Left + Margin.Right + Padding.Left + Padding.Right, FormattedText?.Height??0 + Margin.Top + Margin.Bottom + Padding.Left + Padding.Right);//base.MeasureOverride(constraint);
        }

        protected override Size ArrangeOverride(Size arrangeBounds)
        {
            Console.WriteLine($"{Content}:ArrangeOverride={arrangeBounds.Width},{arrangeBounds.Height}");
            ArrangeWidth = arrangeBounds.Width;
            ArrangeHeight = arrangeBounds.Height;
            return base.ArrangeOverride(arrangeBounds);
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            Console.WriteLine($"{Content}:ArrangeOverride={FormattedText.Width},{FormattedText.Height}");
            Point pt = new Point(LeftPos,TopPos); 

            


            if (VerticalContentAlignment == VerticalAlignment.Center)
            {
                 pt.Y = (TopPos + BottomPos - FormattedText.Height)/ 2;
            }
            if (HorizontalContentAlignment == HorizontalAlignment.Center)
            {
                 pt.X = (LeftPos + RightPos - FormattedText.Width) / 2;
            }

            drawingContext.DrawText(FormattedText, pt);
        }
    }

}
